package cn.zwx.base.util;

import cn.hutool.core.util.StrUtil;
import cn.zwx.base.constant.ResponseMsg;
import cn.zwx.base.enums.RetCode;
import cn.zwx.base.exception.BizException;

import java.util.List;

public class ParamUtil {


    /** 对象为 null, 或者其字符串形态为 空白符, "null" 时返回 true */
    public static boolean isBlank(Object obj) {
        return obj == null || StrUtil.isEmpty(obj.toString()) || "null".equalsIgnoreCase(obj.toString().trim());
    }

    /** 对象非空时返回 true */
    public static boolean isNotBlank(Object obj) {
        return !isBlank(obj);
    }

    //检查对象是否未空
    public static void checkEmpty(Object obj,String msg){
        checkEmpty(obj, msg, msg);
    }

    /**
     * 如果bool为真，则抛异常
     * @param bool
     * @param msg
     */
    public static void checkTrue(boolean bool ,String msg){
        if (bool){
            throw new BizException(msg);
        }
    }


    /**
     * 检查对象是否未空
     * @param obj
     * @param msg 为空时返回的异常提示信息
     * @param log 打印的日志信息
     */
    public static void checkEmpty(Object obj,String msg,String log){
        if(isBlank(obj)){
            throw new BizException(RetCode.PARAM_ERROR, msg);
        }
    }

    /**
     * 检查list不为null 并且size 大于 0
     * @param obj
     * @param msg
     */
    public static void checkListEmpty(List obj, String msg){
        checkEmpty(obj, msg, msg);
        if (obj.isEmpty()) {
            throw new BizException(RetCode.PARAM_ERROR, msg);
        }
    }

    /**
     * 检查对象是否未空
     * @param obj
     * @param msg 为空时返回的异常提示信息
     * @param log 打印的日志信息
     */
    public static void checkParamEmpty(Object obj,String msg,String log){
        if(isBlank(obj)){
            throw new BizException(msg);
        }
    }
    public static void checkParamEmpty(Object obj,String msg){
        checkEmpty(obj, msg, msg);
    }

    /**
     * 抛出服务异常
     */
    public static void throwBizException(RetCode resp){
        throwBizException( resp.getCode(),resp.getMsg());
    }

    public static void throwBizException(String msg){
        throwBizException(RetCode.FAIL.getCode(), msg);
    }

    public static void throwBizException(String code, String msg){
        throw new BizException(code,msg);
    }

    public static void throwBizException(ResponseMsg msg) {
        throw  new BizException(msg.getCode(), msg.getDesc());
    }

    /**
     * 抛出参数异常
     * @param msg 异常信息
     */
    public static void throwParamException(String msg){
        throwBizException(msg);
    }


}
